package com.isunland.managesystem.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.DatePicker;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.TimeParams;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class SimpleTimeCallBackDialogFragment extends BaseDialogFragment {

  private Calendar mCalendar;
  private String mTitle;
  private CallBack mCallBack;

  @Override public void initData() {
    super.initData();
    TimeParams mParams =
        !(mBaseParams instanceof TimeParams) ? new TimeParams() : (TimeParams) mBaseParams;
    mCalendar = Calendar.getInstance();
    mCalendar.setTime(mParams.getDate() == null ? new Date() : mParams.getDate());
    mTitle = MyStringUtil.isEmpty(mParams.getTitle()) ? "请选择时间" : mParams.getTitle();
  }

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    View view =
        getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_chooseymd_time, null);
    final DatePicker datePicker =
        (DatePicker) view.findViewById(R.id.dp_date_chooseYMDTimeDialogFragment);
    datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
        mCalendar.get(Calendar.DAY_OF_MONTH), null);

    return new AlertDialog.Builder(getActivity()).setTitle(mTitle).setView(view)
        //				mAlertDialog = new AlertDialog.Builder(getActivity())
        //				.setTitle(R.string.chooseAcquireTime).setView(widget)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            Date selectDate = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth()).getTime();
            if (mCallBack != null) mCallBack.confirm(selectDate);
          }
        }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            if (mCallBack != null) mCallBack.cancel();
          }
        }).create();
  }

  public SimpleTimeCallBackDialogFragment setCallBack(CallBack callBack) {
    mCallBack = callBack;
    return this;
  }

  public interface CallBack {
    void confirm(Date selectDate);

    void cancel();
  }
}
